﻿using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Model.CodeModels;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;
using DevelopTool.Model.Enums;
using LiteDB;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DevelopTool.Bll.DbBlls
{
    /// <summary>
    /// 数据库资源分组业务
    /// </summary>
    public class DbSourceGroupBll : BaseBll
    {
        public DbSourceGroupBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();
                col.EnsureIndex(x => x.NodeName);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }


        /// <summary>
        /// 根据数据库连接信息创建分组节点
        /// </summary>
        /// <param name="dbInfo"></param>
        public void CreateDbSourceGroup(DbInfo dbInfo)
        {
            if (dbInfo == null) return;

            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();

                if (!col.Exists(it => it.DbInfo.Id == dbInfo.Id))
                {
                    DbSourceGroup dbSourceGroup = new DbSourceGroup()
                    {
                        NodeName = dbInfo.NodeName,
                        NodeType = MenuNodeType.Folder,
                        NodePid = 0,
                        SourceType = "TDbInfo",
                        DbInfo = dbInfo
                    };
                    col.Insert(dbSourceGroup);
                }
            }
        }



        public List<DbSourceGroup> GetList(MenuNodeType nodeType = MenuNodeType.Unknown)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<DbSourceGroup>().FindAll();

                if (nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }


        /// <summary>
        /// 根据内容关键字查询到id集合
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<DbSourceGroup> GetListByText(string text)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                List<DbSourceGroup> list = new List<DbSourceGroup>();

                var col = db.GetCollection<DbSourceGroup>();

                var idList = col.Find(it => !string.IsNullOrEmpty(it.SourceType) && it.SourceType.IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.Id).ToList();
                var idList2 = col.Find(it => !string.IsNullOrEmpty(it.DataBaseName) && it.DataBaseName.IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.Id).ToList();
                idList.AddRange(idList2);

                foreach (int noteId in idList)
                {
                    list.Add(col.FindOne(it => it.Id == noteId && it.NodeType == MenuNodeType.Node));//只查询节点，不要目录
                }

                return list;
            }
        }

        /// <summary>
        /// 根据id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DbSourceGroup GetById(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();

                var model = col.FindOne(it => it.Id == id);

                return model;

            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public DbSourceGroup Save(DbSourceGroup dbSourceGroup)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();

                dbSourceGroup.UpdateTime = DateTime.Now;
                col.Upsert(dbSourceGroup);
                return col.FindById(dbSourceGroup.Id);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();

                //查询是否有子节点，若有子节点，则不删除。
                if (col.Exists(it => it.NodePid == id)) return false;

                //删除
                var result = col.Delete(id);

                return result;
            }
        }

        public void DeleteAll(int pid)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();

                var childList = col.Find(it => it.NodePid == pid);

                foreach (var child in childList)
                {
                    DeleteAll(child.Id);

                    col.Delete(child.Id);
                }

            }
        }



        public void SetRemark(int id, string remark)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<DbSourceGroup>();

                var model = col.FindOne(it => it.Id == id);

                model.Remark = remark;
                model.UpdateTime = DateTime.Now;

                col.Update(model);
            }
        }

        public ManageMenu Get(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                return db.GetCollection<DbSourceGroup>().FindOne(it => it.Id == id);
            }
        }

    }
}
